<?php
include("adodb5/adodb-exceptions.inc.php");
include('adodb5/adodb.inc.php');

$data = $_REQUEST['data'];
$source = $_REQUEST['source'];
$strHtmlOutput = '';

switch($source)
{
	case 'usmi':
	//
	$strSql = "SELECT	USR.id_principal AS id, 
						USR.nombre AS responsable, 
						USR.usuario AS usuario, 
						USR.clave AS clave, 
						USR.activo AS status 
						FROM influenza.dbo.usuarios AS USR 
						INNER JOIN Catalogos.dbo.unidades_salud AS US 
						ON USR.id_unidad = US.id_unidad 
						WHERE US.id_unidad = $data 
						ORDER BY USR.nombre, USR.usuario ASC";
		break;
	case 'clue':
	//
	$strSql = 	"SELECT USR.id_principal AS id, USR.nombre AS responsable, USR.usuario AS usuario, USR.clave AS clave, USR.activo AS status 
				FROM influenza.dbo.usuarios AS USR 
				INNER JOIN Catalogos.dbo.unidades_salud AS USSA
				ON USR.id_unidad = USSA.id_unidad WHERE USSA.clues LIKE '$data' 
				UNION ALL
				SELECT USR.id_principal AS id, USR.nombre AS responsable, USR.usuario AS usuario, USR.clave AS clave, USR.activo AS status 
				FROM influenza.dbo.usuarios AS USR 
				INNER JOIN Catalogos.dbo.unidades AS UIST
				ON USR.id_unidad = UIST.id_unidad WHERE UIST.clues LIKE '$data'
				UNION ALL
				SELECT USR.id_principal AS id, USR.nombre AS responsable, USR.usuario AS usuario, USR.clave AS clave, USR.activo AS status 
				FROM influenza.dbo.usuarios AS USR 
				INNER JOIN Catalogos.dbo.unidades_imss AS UIMS
				ON USR.id_unidad = UIMS.id_unidad WHERE UIMS.clues LIKE '$data'";
				
		break;
	case 'usuario':
	//
	$strSql = 	"SELECT	USR.id_principal AS id, USR.nombre AS responsable, USR.usuario AS usuario, USR.clave AS clave, USR.activo AS status 
				FROM influenza.dbo.usuarios AS USR 
				INNER JOIN Catalogos.dbo.unidades_salud AS USSA
				ON USR.id_unidad = USSA.id_unidad 
				WHERE USR.usuario LIKE '$data' 
				UNION ALL
				SELECT	USR.id_principal AS id, USR.nombre AS responsable, USR.usuario AS usuario, USR.clave AS clave, USR.activo AS status 
				FROM influenza.dbo.usuarios AS USR 
				INNER JOIN Catalogos.dbo.unidades AS UIST
				ON USR.id_unidad = UIST.id_unidad 
				WHERE USR.usuario LIKE '$data' 
				UNION ALL
				SELECT	USR.id_principal AS id, USR.nombre AS responsable, USR.usuario AS usuario, USR.clave AS clave, USR.activo AS status 
				FROM influenza.dbo.usuarios AS USR 
				INNER JOIN Catalogos.dbo.unidades_imss AS UIMS
				ON USR.id_unidad = UIMS.id_unidad 
				WHERE USR.usuario LIKE '$data'";
		break;
}

//print "$strSql";
//exit();

$strDsn = 'mssql://influenza:inf123456@10.1.251.14';
$strHtmlOutput = '';

$cnn = ADONewConnection($strDsn);
$cnn->SetFetchMode(ADODB_FETCH_ASSOC);

if($cnn)
{
//
	$rs = $cnn->Execute($strSql);
	
	if($rs)
	{
	//
		$intNoRows = $rs->RecordCount();
		
		if($intNoRows > 0)
		{
		//
			$strHtmlOutput .=	'<div style="height: 150px; overflow: auto;"><br>
									<div>Este instituto ya cuenta con ('.$intNoRows.') registros.<br></div>
									<div style="width: 100%;">
										<table class="gridTitles td" width="100%"  border="0" cellpadding="0" cellspacing="0">
											<tr class="gridTitles" width="780" height="20" >
												<td><strong>ID</strong></td>
												<td><strong>RESPONSABLE</strong></td>
												<td><strong>USUARIO</strong></td>
												<td><strong>CLAVE</strong></td>
												<td><strong>STATUS</strong></td>
												<td><div style=" text-decoration: none;"></div></td>
											</tr>';
			
			while(!$rs->EOF)
			{
			//
				$rs->FetchInto($arrCurrentRow);
				
				$strId = $arrCurrentRow['id'];
				$strResponsable = htmlentities($arrCurrentRow['responsable']);
				$strUsuario = $arrCurrentRow['usuario'];
				$strClave = $arrCurrentRow['clave'];
				$strStatus = $arrCurrentRow['status'];
				
				$strHtmlOutput .=	'<tr class="gridRowsHover" width="780" height="20" ><td>'.$strId.'</td><td>'.$strResponsable.'</td><td>'.$strUsuario.'</td><td>'.$strClave.'</td><td>'.$strStatus.'</td><td><div onclick="javascript: userEdit({responsable: \''.$strResponsable.'\', idPrincipal: '.$strId.'});" style=" color: #09C; cursor: pointer; font-weight: bolder;">editar</div></td></tr>';

			}
			
			$strHtmlOutput .= '</table></div><div><br>Este instituto ya cuenta con ('.$intNoRows.') registros</div></div>';
			
			print $strHtmlOutput;
			
		} else {
		//
			$strHtmlOutput .= '<div style="height: 150px; overflow: auto;" align="center"><div style="width: 100%;" align="center"><table width="100%"  border="0" cellpadding="0" cellspacing="0" align="center"><tr width="780" height="20" align="center" ><td align="center">No se encontraron registros</td></tr></table></div></div>';
			
			print "$strHtmlOutput";
		}
		
		$rs->Close();
		
	} else {
	//
		print "rs fail";
	}
	
	$cnn->Close();
	
} else {
//
	print "fail";
}









//print "$strSql";

/*
print "<div>resultas from - Data: $data, source: $source</div>";
print '<div style="height: 150px; overflow: auto;"><div style="width: 100%;"><table width="100%"  border="0" cellpadding="0" cellspacing="0"><tr width="780" height="20" ><td>&nbsp;Id</td><td>&nbsp;Responsable</td><td>&nbsp;Usuario</td><td>&nbsp;Clave</td><td>&nbsp;Status</td></tr>
      <tr width="780" height="20" ><td>&nbsp;a</td><td>&nbsp;b</td><td>&nbsp;c</td><td>&nbsp;d</td><td>&nbsp;e</td></tr>
      <tr width="780" height="20" > 
        <td>&nbsp;a</td>
        <td>&nbsp;b</td>
        <td>&nbsp;c</td>
        <td>&nbsp;d</td>
        <td>&nbsp;e</td>
      </tr>
      <tr width="780" height="20" > 
        <td>&nbsp;a</td>
        <td>&nbsp;b</td>
        <td>&nbsp;c</td>
        <td>&nbsp;d</td>
        <td>&nbsp;e</td>
      </tr>
      <tr width="780" height="20" > 
        <td>&nbsp;a</td>
        <td>&nbsp;b</td>
        <td>&nbsp;c</td>
        <td>&nbsp;d</td>
        <td>&nbsp;e</td>
      </tr>
      <tr width="780" height="20" > 
        <td>&nbsp;a</td>
        <td>&nbsp;b</td>
        <td>&nbsp;c</td>
        <td>&nbsp;d</td>
        <td>&nbsp;e</td>
      </tr>
      <tr width="780" height="20" > 
        <td>&nbsp;a</td>
        <td>&nbsp;b</td>
        <td>&nbsp;c</td>
        <td>&nbsp;d</td>
        <td>&nbsp;e</td>
      </tr>
      <tr width="780" height="20" > 
        <td>&nbsp;a</td>
        <td>&nbsp;b</td>
        <td>&nbsp;c</td>
        <td>&nbsp;d</td>
        <td>&nbsp;e</td>
      </tr>
      <tr width="780" height="20" > 
        <td>&nbsp;a</td>
        <td>&nbsp;b</td>
        <td>&nbsp;c</td>
        <td>&nbsp;d</td>
        <td>&nbsp;e</td>
      </tr>
      <tr width="780" height="20" > 
        <td>&nbsp;a</td>
        <td>&nbsp;b</td>
        <td>&nbsp;c</td>
        <td>&nbsp;d</td>
        <td>&nbsp;e</td>
      </tr>
      <tr width="780" height="20" > 
        <td>&nbsp;a</td>
        <td>&nbsp;b</td>
        <td>&nbsp;c</td>
        <td>&nbsp;d</td>
        <td>&nbsp;e</td>
      </tr>
      <tr width="780" height="20" > 
        <td>&nbsp;a</td>
        <td>&nbsp;b</td>
        <td>&nbsp;c</td>
        <td>&nbsp;d</td>
        <td>&nbsp;e</td>
      </tr>                                                        
    </table></div></div>';

*/
/*


$intEntidad = $_REQUEST['intEntidad'];



if($cnn)
{
//
	$strSql = "SELECT	US.id_unidad AS id_unidad, 
						US.clues, 
						'(' + ENT.corto + ') - ' + US.nombre AS nombre 
						FROM Catalogos.dbo.unidades_salud AS US 
						INNER JOIN Catalogos.dbo.entidad AS ENT 
						ON ENT.entidad = US.id_entidad 
						WHERE id_entidad = $intEntidad 
						AND activo = 1 
						AND LEN(clues) = 11 
						ORDER BY US.nombre";
	$rs = $cnn->Execute($strSql);
	
	if($rs)
	{
	//
		$intNoRows = $rs->RecordCount();
		
		if($intNoRows > 0)
		{
		//
			$strHtmlOutput .=	'<label for="cboUsmiNameList">Seleccione una USMI:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
								<select name="cboUsmiNameList" id="cboUsmiNameList" onchange="javascript: alert($(\'cboUsmiNameList\').options[$(\'cboUsmiNameList\').selectedIndex].value);">';
			
			while(!$rs->EOF)
			{
			//
				$rs->FetchInto($arrCurrentRow);
				$strIdUnidad = $arrCurrentRow['id_unidad'];
				$strNombre = htmlentities($arrCurrentRow['nombre']);
				$strHtmlOutput .=	"<option value=\"$strIdUnidad\">$strNombre</option>\n";

			}
			
			$strHtmlOutput .= '</select>';
			
			print $strHtmlOutput;
			
		} else {
		//
		}
		
		$rs->Close();
		
	} else {
	//
		print "rs fail";
	}
	
	$cnn->Close();
	
} else {
//
	print "fail";
}
*/
?>